<?php
class WallpapersController extends AppController {
	var $name = 'Wallpapers';

    public function remove () {
        if (!$this->_checkLogged('A')) {
            return;
        }

        $id   = $this->params['form']['id'];
        $data = $this->Wallpaper->read(array('filename','created'), $id);

        if (count($data) == 0) {
            $json = array(
                'status' => false,
                'error'  => __('Fail delete wallpaper, please try again.', true)
            );
            $this->response($json);
            return;
        }

        if (!$this->Wallpaper->delete($id)) {
            $json = array(
                'status' => false,
                'error'  => __('Fail delete wallpaper, please try again.', true)
            );
            $this->response($json);
            return;
        }

        $path = IMAGES.'wallpapers'.DS;

        @unlink($path.$data['Wallpaper']['filename']);
        @unlink($path.'1280_'.$data['Wallpaper']['filename']);
        @unlink($path.'800_'.$data['Wallpaper']['filename']);

        $json = array('status' => true);
        $this->response($json);
    } // remove

    public function axUpload () {
        if (!$this->_checkLogged('A')) {
            return;
        }

        set_time_limit(0);
        Configure::write('debug', 0);

        App::import('Vendor', 'ImageClass', array('file'=>'image.class.php'));

        $post = $this->params['form'];
        $name = basename($_FILES['uploadImage']['tmp_name']);

        list($width, $height, $mime,) = @getimagesize($_FILES['Filedata']['tmp_name']);

        $img = new Upload($_FILES['uploadImage']);

        $img->allowed            = array('image/jpeg', 'image/pjpeg', 'image/gif', 'image/png');
        $img->file_new_name_body = $name;
        $img->image_min_width    = 64;
        $img->image_min_height   = 64;
        $img->image_x            = 180;
        $img->image_y            = 180;
        $img->image_ratio_crop   = true;
        $img->image_resize       = true;
        $img->Process(IMAGES.'wallpapers'.DS);

        if (!$img->processed) {
            $json = array(
                'status'  => -1,
                'message' => __($img->error, true)
            );
            $this->set('json', $json);
            $this->render(false, 'json');
            return;
        }
        $thumbnail = $img->file_dst_name;

        if ($post['format'] == 'pc') {
            $sizeW = 1280;
            $sizeH = 1024;
            $prefx = '1280_';
        } else {
            $sizeW = 320;
            $sizeH = 480;
            $prefx = 'iphone_';
        }
        $img->file_new_name_body = $prefx.$name;
        $img->image_x            = $sizeW;
        $img->image_y            = $sizeH;
        $img->image_ratio_crop   = true;
        $img->image_resize       = true;
        $img->Process(IMAGES.'wallpapers'.DS);

        if (!$img->processed) {
            $json = array(
                'status'  => -1,
                'message' => __($img->error, true)
            );
            $this->set('json', $json);
            $this->render(false, 'json');
            return;
        }

        if ($post['format'] == 'pc') { // one more size for pc format
            $img->file_new_name_body = '800_'.$name;
            $img->image_x            = 800;
            $img->image_y            = 600;
            $img->image_ratio_crop   = true;
            $img->image_resize       = true;
            $img->Process(IMAGES.'wallpapers'.DS);

            if (!$img->processed) {
                $json = array(
                    'status'  => -1,
                    'message' => __($img->error, true)
                );
                $this->set('json', $json);
                $this->render(false, 'json');
                return;
            }
        }
        $img->Clean();

        $data = array(
            'filename' => $thumbnail,
            'format'   => ($post['format'] == 'pc') ? 'P' : 'I'
        );

        if ($this->Wallpaper->save($data)) {
            $json = array(
                'status'    => 1,
                'id'        => $this->Wallpaper->id,
                'thumbnail' => $thumbnail,
                'format'    => ($post['format'] == 'pc') ? 'PC' : 'iPhone'
            );
        } else {
            $json = array(
                'status'  => -1,
                'message' => __('Error while insert slide record to database.', true)
            );
        }

        $this->set('json', $json);
        $this->render(false, 'json');
        sleep(1);
    } // axUpload

    public function manage () {
        if (!$this->_checkLogged('A')) {
            return;
        }

        $data = $this->Wallpaper->find('all', array(
            'order' => array('format DESC', 'order ASC', 'id DESC')
        ));

        $this->javascripts = am($this->javascripts, 'webtoolkit.aim');
        $this->pageTitle   = __('Manage Wallpapers',true);
        $this->set('data', $data);
    } // manage

    public function edit () {
        $form = $this->params['form'];

        foreach ($form as $k=>$v) {
            if (strpos($k, 'rder_')) {
                $id = str_replace('order_', '', $k);

                $this->Wallpaper->id = $id;
                $this->Wallpaper->saveField('order', $v);
            }
        }

        $this->Session->setFlash('<h4>'.__('Wallpapers has been updated.',true).'</h4>', 'default', array('class'=>'boxSuccess'));
        $this->redirect('/wallpapers/manage');
    } // edit

    public function index () {
        $params = array(
            'recursive' => -1,
            'limit'     => 15,
            'order'     => array('format DESC', 'order ASC', 'id DESC')
        );
        $this->paginate = $params;

        $this->stylesheet  = am($this->stylesheet, 'sexylightbox/sexylightbox');
        $this->javascripts = am($this->javascripts, 'http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js', 'jquery.easing.1.3', 'sexylightbox.v2.3.jquery.min');
        $this->pageTitle = __('Wallpaper',true);
        $this->set('data', $this->paginate());
    } // index

    public function download ($size, $filename) {
        $this->view = 'Media';

        $ext = pathinfo($filename, PATHINFO_EXTENSION);

        $params = array(
            'id'        => $size.'_'.$filename,
            'name'      => $size.'_wallpaper',
            'download'  => true,
            'extension' => $ext,
            'path'      => IMAGES.'wallpapers'.DS
        );

        $this->set($params);
    } // download
} // WallpapersController
?>